﻿using Microsoft.Xna.Framework;
using Ultima.Xna.Data;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public class GumpTextEntry : GumpEntry
    {
        private string _text = string.Empty;
        private int _entryId;
        private int _hue = 1;
        private int _fontId = 9;
        private bool _passwordMask;

        public bool PasswordMask
        {
            get { return _passwordMask; }
            set { _passwordMask = value; }
        }

        public int FontId
        {
            get { return _fontId; }
            set { _fontId = (int)MathHelper.Clamp(value, 0, 10); }
        }

        public ASCIIFont Font
        {
            get { return ASCIIFont.GetFixed(_fontId); }

        }

        public override string Text
        {
            get
            {
                if (Dock != Dock.None)
                {
                    return Utility.WrapASCIIText(_fontId, base.Text, Bounds.Width);
                }

                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        public int Hue
        {
            get { return _hue; }
            set { _hue = value; }
        }

        public int EntryId
        {
            get { return _entryId; }
            set { _entryId = value; }
        }

        public GumpTextEntry(Game game)
            : base(game)
        {

        }

        public override void Draw(GameTime gameTime)
        {
            Manager.Renderer.Render(this);

            ////TODO: Fix fucking hueing... I'm only getting white, or black...
            //GumpEffect.Parameters["HueTexture"].SetValue(Hues.Texture);
            //GumpEffect.Parameters["Hue"].SetValue(new Vector2(Hue - 1, 0));

            //string text = _text;

            //if (Mask)
            //{
            //    string temp = string.Empty;

            //    for (int i = 0; i < text.Length; i++)
            //    {
            //        temp += '*';
            //    }

            //    text = temp;
            //}

            //if (HasFocus)
            //{
            //    text += "_";
            //}

            //ControlRenderer.RenderASCIIText(this, GumpEffect, 9, text);

            base.Draw(gameTime);
        }

        public override void Invalidate()
        {
            _size = new Vector2(Font.GetWidth(Text), Font.Height);

            base.Invalidate();

            Vector2 position = Position;

            if (Dock == Dock.None)
            {
                RenderBounds = new Rectangle((int)position.X, (int)position.Y, Font.GetWidth(Text), Font.Height);
            }
        }

        protected override void OnKeyDown(object sender, KeyboardEventArgs e)
        {
            Input.HandleKeyboardInput(ref _text);

            base.OnKeyDown(sender, e);
        }
    }
}
